package com.honeywen.mymvc.test;

import org.omg.SendingContext.RunTime;

/**
 * @author wangwei
 * @date 2019/3/21
 */
public class Singleton {


    private static final String SINGLETON_PATH = "com.honeywen.mymvc.test.Singleton.getInstance(Singleton.java";
    private Singleton() {

        boolean flag = false;

        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
        for (StackTraceElement el: stackTrace) {
            String str = el.toString();

            if (str.contains(SINGLETON_PATH)) {
                flag = true;
                break;
            }

        }
        if (!flag) {
            throw new RuntimeException("not correct way to get singleton");
        }
    }

    public static Singleton getInstance() {
        return SingletonHolder.singleton;
    }

    private static class SingletonHolder {
        private static final Singleton singleton = new Singleton();
    }
}
